<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Warehouse extends Backend
{
    
    /**
     * UserWarehouse模型对象
     */
    protected $model = null;
    protected $warehouse_items;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user_warehouse');
        $this->warehouse_items = config('user_warehouse');

    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $total = $this->model
                ->alias('uw')
                ->join('users u', 'u.user_id = uw.user_id')
                ->where($where)
                ->order($sort, $order)
                ->count();
            
            $list = $this->model
                ->alias('uw')
                ->join('users u', 'u.user_id = uw.user_id')
                ->field('uw.*, u.full_name, u.user_name')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            
            foreach ($list as &$v) {
                if(isset($v['item'])) {
                    $v['item_name'] = $this->warehouse_items[$v['item']]['value'];
                }
            }
            unset($v);
            
            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        
        $btns = ['refresh'];
        
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    /**
     * 详情
     */
    public function detail($ids = null){
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $warehouse_id = input('ids');
            
            $warehouse = Db::name('user_warehouse')->field('user_id, item')->where(['id' => $warehouse_id])->find();
            
            $where = array();
            $where['user_id'] = $warehouse['user_id'];
            $where['item'] = $warehouse['item'];
            
            $total = Db::name('user_warehouse_log')
                ->where($where)
                ->order($sort, $order)
                ->count();
        
            $list = Db::name('user_warehouse_log')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
        
            foreach ($list as &$v) {
                if(isset($v['item'])) {
                    $v['item_name'] = $this->warehouse_items[$v['item']]['value'];
                }
            }
            unset($v);
        
            $result = array("total" => $total, "rows" => $list);
        
            return json($result);
        }
    
        $btns = ['refresh'];
        $this->view->assign('btns', $btns);
        $this->view->assign('id', $ids);
        return $this->view->fetch();
    }

}
